package com.zrwl.poscloud.xo.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zrwl.poscloud.commons.utils.StringUtils;

import java.util.List;

public class TreeUtil {

    public static String buildTreeJsonStrByList(List dataList, String[] columnNameArray, String[] defaultNameArray, boolean setDefaultName) {
        JSONArray dataListJson = JSONArray.parseArray(JSONObject.toJSONString(dataList));

        JSONArray resultArray = new JSONArray();
        JSONArray[] nowChildrenLists = new JSONArray[columnNameArray.length];
        String[] nowColumnValue = new String[columnNameArray.length];
        for (int i = 0; i < dataListJson.size(); i++) {
            JSONObject dataRow = dataListJson.getJSONObject(i);
            for (int j = 0; j < columnNameArray.length; j++) {
                String column = columnNameArray[j];
                String columnValue = dataRow.getString(column);
                if (StringUtils.isEmpty(columnValue)) {
                    if (setDefaultName)
                        columnValue = defaultNameArray[j];
                    else
                        continue;
                }
                if (columnValue.equals(nowColumnValue[j])) {
                    //此行的列数据和上一次的列一致，则进入同一级
                } else {
                    //此行的列数据和上一次的列不一致，则创建新的数据到该等级的childrenList
                    for (int m = j; m < columnNameArray.length; m++) {
                        nowChildrenLists[m] = new JSONArray();
                        String columnValueTemp = dataRow.getString(columnNameArray[m]);
                        if (StringUtils.isEmpty(columnValueTemp) && setDefaultName) {
                            columnValueTemp = defaultNameArray[m];
                        }
                        nowColumnValue[m] = columnValueTemp;
                    }
                    JSONObject newTreeNode = getNewTreeNodeObject(dataRow, nowChildrenLists, columnNameArray, defaultNameArray, setDefaultName, j);
                    if (j == 0) {
                        resultArray.add(newTreeNode);
                    } else {
                        while (j > 0 && nowChildrenLists[--j] == null) {
                            //定位一个上级不是空的节点
                        }
                        if (nowChildrenLists[j] == null) {
                            resultArray.add(newTreeNode);
                        } else {
                            nowChildrenLists[j].add(newTreeNode);
                        }

                    }
                    break;
                }
            }
        }
        return resultArray.toJSONString();
    }

    //从某一层级开始创建一套完整的父子结构直到叶节点
    static JSONObject getNewTreeNodeObject(JSONObject dataRow, JSONArray[] nowChildrenLists, String[] columnNameArray, String[] defaultNameArray, boolean setDefaultName, int nowLevel) {
        JSONObject[] everyLevelBean = new JSONObject[columnNameArray.length];
        for (int j = nowLevel; j < columnNameArray.length; j++) {
            String column = columnNameArray[j];
            String columnValue = dataRow.getString(column);
            if (StringUtils.isEmpty(columnValue)) {
                if (setDefaultName)
                    columnValue = defaultNameArray[j];
                else
                    continue;
            } else {
                if (setDefaultName) {
                    switch (column) {
                        case "region":columnValue = columnValue+"区"; break;
                        case "build":columnValue = columnValue+"楼";  break;
                        case "unit":columnValue = columnValue+"单元";  break;
                        case "room":columnValue = columnValue+"房间"; break;
                    }
                }
            }
            JSONObject object = getNewObject(dataRow.getString("uid"), columnValue, column, dataRow);
            everyLevelBean[j] = object;
            int x = j;
            while (x > nowLevel && everyLevelBean[--x] == null) {
                //定位一个上级不是空的节点
            }
            if (j > (nowLevel))//当后面的数据加载时，把此数据加入到前面的子节点列表中
                everyLevelBean[x].getJSONArray("children").add(object);
            nowChildrenLists[j] = object.getJSONArray("children");
        }
        return everyLevelBean[nowLevel];
    }

    static JSONObject getNewObject(String uid, String text, String columnName, JSONObject dataRow) {
        JSONObject result = new JSONObject();
        result.put("uid", uid);
        result.put("label", text);
        result.put("value", uid);
//        result.put("data", dataRow);
        result.put("columnName", columnName);
        result.put("summary", dataRow.getString("summary"));
        result.put("name", dataRow.getString("name"));
        result.put("oldName", dataRow.getString("oldName"));
        if(columnName.equals("room")){
            result.put("isDisabled", dataRow.getIntValue("status") == 2);
        }
        result.put("children", new JSONArray());
        return result;
    }

}
